package info.sybian.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * LGPL V3
 * 
 * @author nkadun
 */
public class EncodingFilter implements Filter {

	private static final String ENCODING_NAME = "encoding";

	private String encoding = "UTF-8";

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws ServletException, IOException {
		req.setCharacterEncoding(encoding);
		fc.doFilter(req, res);
	}

	@Override
	public void init(FilterConfig c) throws ServletException {
		String v = c.getInitParameter(ENCODING_NAME);
		if(v != null && v.length() > 0)
			encoding = v;
	}
}
